package com.qiaofang.collectmanage.dao.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Description: 日期时间工具类
 */
public class DateUtils {

    private static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    private DateUtils(){}

    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_T_HH_MM_SS_SSS = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    public static final String YYYY_MM_DD_T_HH_MM_SS_SSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    /**
     * 解析字符串为时间
     */
    public static String parseDateYYYY_MM_DD(String input) throws Exception {
        SimpleDateFormat pr = new SimpleDateFormat(YYYYMMDD);
        SimpleDateFormat fm = new SimpleDateFormat(YYYY_MM_DD);
        return fm.format(pr.parse(input));
    }
    /**
     * 格式化日期
     */
    public static String formatDate(Date date, String format) throws Exception {
        SimpleDateFormat fm = new SimpleDateFormat(format);
        return fm.format(date);
    }

    public static String dayAddNum(String date, int num) throws Exception {
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        c.setTime(f.parse(date));
        c.add(Calendar.DAY_OF_MONTH, num);
        Date tomorrow = c.getTime();
        return f.format(tomorrow);
    }

    /**
     * 当天开始时间
     * @return
     */
    public static Date getDayStartDate(){
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY,0);
        todayStart.set(Calendar.MINUTE,0);
        todayStart.set(Calendar.SECOND,0);
        todayStart.set(Calendar.MILLISECOND,0);
        return todayStart.getTime();
    }

    /**
     * 当天开始时间
     * + 8 小时
     * @return
     */
    public static Date getDayStartDateTime(){
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY,0);
        todayStart.set(Calendar.MINUTE,0);
        todayStart.set(Calendar.SECOND,0);
        todayStart.set(Calendar.MILLISECOND,0);
        todayStart.add(Calendar.HOUR_OF_DAY, +8);
        return todayStart.getTime();
    }


    /**
     * 当天结束时间
     * @return
     */
    public static Date getDayEndDate() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    /**
     * 当天结束时间
     * + 8小时
     * @return
     */
    public static Date getDayEndDateTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        todayEnd.add(Calendar.HOUR_OF_DAY, +8);
        return todayEnd.getTime();
    }


    public static String getDayStartTime()throws Exception{
        return formatDate(getDayStartDate(), YYYY_MM_DD_T_HH_MM_SS_SSS);
    }

    public static String getDayEndTime()throws Exception{
        return formatDate(getDayEndDate(), YYYY_MM_DD_T_HH_MM_SS_SSS);
    }

    public static String getNowDate () {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat(YYYY_MM_DD);
        // new Date()为获取当前系统时间
        String nowDate = df.format(new Date());
        return nowDate;
    }

    /**
     * 将String类型的时间戳转换成String类型的时间格式
     * 时间格式为：yyyy-MM-dd HH:mm:ss
     * @param time String类型的时间戳
     * @return
     */
    public static String convertTimeToString(String time){
        String returnTime = null;
        if (StringUtils.isNotBlank(time)) {
            long timeLong = Long.valueOf(time);
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD_T_HH_MM_SS_SSSZ);
            returnTime = sdf.format(new Date(timeLong));
        }
        return returnTime;
    }

    /**
     * 格式化日期
     */
    public static String formatDateToZone(Date date){
        String time = "";
        try {
            SimpleDateFormat fm = new SimpleDateFormat(YYYY_MM_DD_T_HH_MM_SS_SSSZ);
            time = fm.format(date);
        }catch(Exception e){
            logger.error("日期解析错误{}",e);
        }
        return time;
    }
}
